<template>
  <div class="content">
    <search-box></search-box>
    <vSwiper :bannerList="list"></vSwiper>
    <div class="main-box mgt-27">
      <div class="width1200 margin-center">
        <div class="mgb-40">
          <img src="../assets/img/index/ad_banner.png" alt="" class="ads" />
        </div>
        <div>
          <div class="com-tag flex-between-center mgb-20">
            <div class="title-tag">我的{{ isoff == 1 ? "培训" : "研修" }}</div>
            <div
              class="more-tag flex-start-center"
              @click="
                {
                  $router.push('/myTraining/' + isoff + '/1');
                }
              "
            >
              <p>查看更多</p>
              <img
                src="../assets/img/index/more.png"
                alt=""
                class="more-Img mgl-5"
              />
            </div>
          </div>
          <div class="learning-box flex-center mgb-40">
            <div
              v-if="signTableData.length == 0"
              class="center mg-50 color-999"
            >
              暂无数据
            </div>
            <div
              class="learn-child"
              v-for="item in signTableData"
              :key="item.trainId"
              @click="
                {
                  $router.push('/trainingDetail/' + isoff + '/' + item.trainId);
                }
              "
            >
              <el-image
                class="learning-Img mgb-10"
                :src="item.headImg"
                fit="cover"
              ></el-image>
              <p class="state" v-if="item.state == 1">
                <img class="icon" :src="inHand_icon" />进行中
              </p>
              <p class="state" v-if="item.state == 2">
                <img class="icon" :src="waiting_icon" />待开始
              </p>
              <p class="state" v-if="item.state == 3">
                <img class="icon" :src="over_icon" />已结束
              </p>
              <p class="mgl-20 title mgb-10 line1">{{ item.title }}</p>
              <p class="mgl-20 date-text mgb-17 f12">
                <span class="mgr-15 color-666">{{ item.signNum }}已报名</span>
                <span class="color-666"
                  >{{ item.addtime.substring(0, item.addtime.length - 3) }}~{{
                    item.begintime.substring(0, item.begintime.length - 3)
                  }}</span
                >
              </p>
            </div>
          </div>
          <div class="com-tag flex-between-center mgb-20">
            <div class="title-tag">线下{{ isoff == 1 ? "培训" : "研修" }}</div>
            <div
              class="more-tag flex-start-center"
              @click="
                {
                  $router.push('/myTraining/' + isoff + '/2');
                }
              "
            >
              <p>查看更多</p>
              <img
                src="../assets/img/index/more.png"
                alt=""
                class="more-Img mgl-5"
              />
            </div>
          </div>
          <div class="new-box flex-center mgb-40">
            <div v-if="tableData.length == 0" class="center mg-50 color-999">
              暂无数据
            </div>
            <div
              class="learn-child"
              v-for="item in tableData"
              :key="item.trainId"
              @click="
                {
                  $router.push('/trainingDetail/' + isoff + '/' + item.trainId);
                }
              "
            >
              <el-image
                class="learning-Img mgb-10"
                :src="item.headImg"
                fit="cover"
              ></el-image>
              <p class="state" v-if="item.state == 1">
                <img class="icon" :src="inHand_icon" />进行中
              </p>
              <p class="state" v-if="item.state == 2">
                <img class="icon" :src="waiting_icon" />待开始
              </p>
              <p class="state" v-if="item.state == 3">
                <img class="icon" :src="over_icon" />已结束
              </p>
              <p class="mgl-20 title mgb-10 line1">{{ item.title }}</p>
              <div class="mgl-20 flex-between-center">
                <p class="tag">{{ item.school }}</p>
                <div
                  v-if="item.isSign == 0"
                  class="btn focus"
                  @click.stop="joinServe(item)"
                >
                  报名
                </div>
                <div v-if="item.isSign == 1" class="btn">已报名</div>
              </div>
              <p class="mgl-20 date-text mgb-17 f12">
                <span class="mgr-15 color-666">{{ item.signNum }}已报名</span>
                <span class="color-666"
                  >{{ item.addtime.substring(0, item.addtime.length - 3) }}~{{
                    item.begintime.substring(0, item.begintime.length - 3)
                  }}</span
                >
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 报名弹层 -->
    <popContent
      v-if="dialogVisibleScore"
      :pop-width="1000"
      :full-screen-icon="true"
      :btn-show="false"
      title="报名"
      @closePop="dialogVisibleScore = false"
    >
      <div class="content-box">
        <div class="tel-box">
          <p class="tel-label">联系电话:</p>
          <el-input
            v-model="joinTel"
            placeholder="请输入电话号码"
            class="input-box"
          ></el-input>
        </div>
        <div class="btns">
          <div class="close-btn" @click="dialogVisibleScore = false">
            再想想
          </div>
          <div class="join-btn" @click="getTrainToSign()">确认</div>
        </div>
      </div>
    </popContent>
    <popContent
      v-if="dialogVisibleJoined"
      :pop-width="1000"
      :full-screen-icon="true"
      :btn-show="false"
      title=""
      @closePop="dialogVisibleJoined = false"
    >
      <div class="content-box-joined">
        <img src="../assets/img/success.png" alt="" class="success-img" />
        <p class="joined-text">报名成功</p>
      </div>
    </popContent>
  </div>
</template>
<script>
import SearchBox from "../components/SearchBox";
import popContent from "@/components/popContent";
import vSwiper from "../components/swiper";
import pager from "../components/pager";

export default {
  name: "trainingService",
  components: {
    SearchBox,
    vSwiper,
    pager,
    popContent,
  },
  data() {
    return {
      list: [],
      joinTel: "",
      isoff: "", //1培训，2研修
      dialogVisibleJoined: false,
      dialogVisibleScore: false,
      waiting_icon: require("../assets/img/椭圆 3@2x.png"),
      inHand_icon: require("../assets/img/进行中@2x.png"),
      over_icon: require("../assets/img/index/椭圆 2 副本@2x.png"),
      signTableData: [],
      tableData: [],
    };
  },
  created() {
    this.init();
  },
  watch: {
    // 监听路由变化，路由变化的时候请求页面数据
    $route: function (to, from) {
      this.init();
    },
  },
  methods: {
    init() {
      this.isoff = this.$route.params.id;
      if (!this.isoff) {
        return this.$message.warning("参数不合法！");
      }
      this.getTrainMySignList();
      this.getTrainAllList();
      this.getBannerList();
    },
    // banner 7培训 、8研修
    getBannerList() {
      let id = this.isoff == 1 ? 7 : 8;
      this.$api.banner
        .list({
          position: id,
        })
        .then((d) => {
          if (d.code == 200) {
            this.list = d.data;
          } else {
            this.$message.error(d.msg);
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    // 我的报名培训
    getTrainMySignList() {
      this.$api.train
        .getTrainMySignList({
          current: 1,
          size: 3,
          state: "",
          type: this.isoff,
        })
        .then((d) => {
          if (d.code == 200) {
            this.signTableData = d.data;
          } else {
            this.$message.error(d.msg);
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    //培训研修首页
    getTrainAllList() {
      this.$api.train
        .getTrainAllList({
          current: 1,
          size: 3,
          state: "",
          type: this.isoff,
        })
        .then((d) => {
          if (d.code == 200) {
            this.tableData = d.data;
          } else {
            this.$message.error(d.msg);
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    //报名
    getTrainToSign() {
      this.$api.train
        .getTrainToSign({
          trainId: this.id,
        })
        .then((d) => {
          if (d.code == 200) {
            this.showJoined();
            this.getTrainMySignList();
            this.getTrainAllList();
          } else {
            this.$message.error(d.msg);
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },

    joinServe(row) {
      this.id = row.trainId;
      this.getTrainToSign();
      // this.dialogVisibleScore = true;
    },
    showJoined() {
      this.dialogVisibleJoined = true;
    },
  },
};
</script>
<style lang="less" scoped>
@import "../assets/css/variable.less";
.success-img {
  width: 100px;
  height: 100px;
}
.joined-text {
  margin-top: 15px;
  height: 20px;
  font-size: 20px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #333333;
}

.content-box {
  display: flex;
  justify-content: space-around;
  align-items: center;
  flex-direction: column;
}
.tel-box {
  .tel-label {
    width: 61px;
    height: 14px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #333333;
  }
  display: flex;
  justify-content: center;
  align-items: center;
  .input-box {
    margin-left: 10px;
    width: 221px;
    height: 35px;
    // border: 1px solid #d8d8d8;
    border-radius: 2px;
  }
}
.btns {
  display: flex;
  justify-content: center;
  align-items: center;
}
.join-btn {
  width: 220px;
  height: 34px;
  background: #cb3e3e;
  border-radius: 2px;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: 20px;
}
.close-btn {
  width: 221px;
  height: 35px;
  border: 1px solid #d8d8d8;
  border-radius: 2px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 20px;
}
.content-box {
  width: 100%;
  height: 250px;
  margin-top: 50px;
  margin-bottom: 50px;
  // border: 1px red solid;
}
.content-box-joined {
  width: 100%;
  height: 250px;
  margin-top: 50px;
  margin-bottom: 50px;
  // border: 1px red solid;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.btn {
  background-color: #cb3e3e;
  opacity: 0.5;
  border-radius: 4px;
  width: 60px;
  text-align: center;
  font-size: 12px;
  color: #fff;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  line-height: 28px;
  height: 28px;
  display: inline-block;
  &.focus {
    opacity: 1;
  }
}
.line2 {
  width: 15px;
  height: 2px;
  background: #cb3e3e;
}
.learning-Img {
  width: 100%;
  height: 213px;
}
.learning-box {
  width: 100%;
}
.learn-child {
  width: 360px;
  margin-right: calc((100% - 1080px) / 2);
  box-shadow: 1px 2px 33px 5px rgba(204, 204, 204, 0.12);
  border-radius: 1px 1px 1px 1px;
  position: relative;
  cursor: pointer;
  &:nth-child(3n) {
    margin-right: 0;
  }
  .shade {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 213px;
    background: #000000b3;
    border-radius: 1px;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    opacity: 0;
    transition: 1s;
  }
  &:hover {
    .shade {
      opacity: 1;
      transition: 1s;
    }
  }
}
.state {
  position: absolute;
  height: 20px;
  line-height: 20px;
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
  background-color: #fff;
  color: #cb3e3e;
  top: 5%;
  left: 0;
  padding: 0 10px;
  font-size: 12px;
  display: flex;
  align-items: center;
}
.icon {
  width: 10px;
  height: 10px;
  display: inline-block;
  margin-right: 5px;
}

.brad-Img {
  width: 30px;
  height: 30px;
}
.messge-text {
  font-size: 14px;
  color: #333333;
  font-family: Microsoft YaHei;
}
.left-message-box,
.right-message-box {
  width: 50%;
}
.more-Img {
  width: 10px;
  height: 12px;
}
.title {
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #333333;
}
.date-text {
  color: #333333;
  font-size: 14px;
}
.main-box {
  // overflow: scroll;
  min-height: 600px;
}
</style>